package com.xuecheng.content.controller;

import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.api.media.model.aliyun.vo.BindTeachplanMediaVO;
import com.xuecheng.content.common.util.UAASecurityUtil;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author dongliang
 */
@Slf4j
@RestController
@RequestMapping("teachplan")
public class TeachplanController {

    @Autowired
    private TeachplanService teachplanService;


    /**
     * 根据课程Id查询课程计划（树形结构）
     *
     * @param courseId
     * @return
     */
    @GetMapping("/{courseId}/tree-nodes")
    public TeachplanDTO queryTreeNodesByCourseId(@PathVariable Long courseId) {
        //1获取令牌id
        Long companyId = UAASecurityUtil.getCompanyId();
        //2.调用service根据Id查询此课程
        TeachplanDTO teachplanDTO = teachplanService.queryTreeNodesByCourseId(courseId, companyId);

        return teachplanDTO;
    }

    /**
     * 创建或修改课程计划信息
     *
     * @param dto 课程计划信息
     * @return TeachplanDTO 课程计划信息
     */
    @PostMapping
    public TeachplanDTO createOrModifyTeachPlan(@RequestBody TeachplanDTO dto) {

        //1获取令牌id
        Long companyId = UAASecurityUtil.getCompanyId();

        TeachplanDTO teachplanDTO = teachplanService.createOrModifyTeachPlan(dto, companyId);

        return teachplanDTO;
    }

    /**
     * 根据课程计划Id删除课程计划信息
     *
     * @param teachPlanId {@link Long} 课程计划Id
     */
    @DeleteMapping("{teachPlanId}")
    public void removeTeachPlan(@PathVariable Long teachPlanId) {
        //1获取令牌id
        Long companyId = UAASecurityUtil.getCompanyId();
        //2.调用service根据Id查询此课程
        teachplanService.removeTeachPlan(teachPlanId, companyId);
    }


    /**
     * 添加视频
     * @param vo 视频与课程计划绑定
     * @return TeachplanMediaDTO  视频与课程计划绑定dto
     */
    @PostMapping("media/association")
    public TeachplanMediaDTO associateMedia(@RequestBody BindTeachplanMediaVO vo) {

        Long companyId = UAASecurityUtil.getCompanyId();

        TeachplanMediaDTO teachplanMediaDTO = new TeachplanMediaDTO();

        teachplanMediaDTO.setTeachplanId(vo.getTeachplanId());
        teachplanMediaDTO.setMediaId(vo.getMediaId());


        TeachplanMediaDTO resultDTO = teachplanService.associateMedia(teachplanMediaDTO, companyId);

        return resultDTO;
    }
}
